<?php

namespace App\Observers;

use App\Events\TopicDeleted;
use App\Events\TopicRestored;
use App\Models\Forum;
use App\Models\Topic;
use App\Models\TopicOperationLog;

class TopicObserver
{

    /**
     * Handle the topic "created" event.
     *
     * @param  \App\Models\Topic  $topic
     * @return void
     */
    public function created(Topic $topic)
    {
        //
    }

    /**
     * Handle the topic "updated" event.
     *
     * @param  \App\Models\Topic  $topic
     * @return void
     */
    public function updated(Topic $topic)
    {
        if ($topic->isDirty('fid')) {
            $oldFid = $topic->getOriginal('fid');
            $newFid = $topic->getAttribute('fid');
            $oldForum = Forum::query()->findOrFail($oldFid);
            $newForum = Forum::query()->findOrFail($newFid);
            $operateLog = [
                'type' => TopicOperationLog::TYPE_MOVE_FORUM,
                'remarks' => sprintf('原块版：%s', $oldForum->name),
                'extra' => ['old' => $oldForum->toArray(), 'new' => $newForum->toArray()]
            ];
            $operateLog = $topic->operationLogs()->create($operateLog);
            $log = sprintf(
                '%s, change fid, old: %s(%s), new: %s(%s), operationLog: %s',
                __METHOD__, $oldForum->id, $oldForum->name, $newForum->id, $newForum->name, $operateLog->id
            );
            logger($log);
        }

        if ($topic->isDirty('sensitivity')) {
            $oldSensitivity = $topic->getOriginal('sensitivity');
            $newSensitivity = $topic->getAttribute('sensitivity');
            $operateLog = [
                'type' => TopicOperationLog::TYPE_CHANGE_SENSITIVITY,
                'extra' => ['old' => $oldSensitivity, 'new' => $newSensitivity]
            ];
            $operateLog = $topic->operationLogs()->create($operateLog);
            $log = sprintf(
                '%s, change sensitivity, old: %s, new: %s, operationLog: %s',
                __METHOD__, $oldSensitivity, $newSensitivity, $operateLog->id
            );
            logger($log);
        }
    }

    /**
     * Handle the topic "deleted" event.
     *
     * @param  \App\Models\Topic  $topic
     * @return void
     */
    public function deleted(Topic $topic)
    {
        $operateLog = [
            'type' => TopicOperationLog::TYPE_DELETE,
        ];
        $operateLog = $topic->operationLogs()->create($operateLog);
        $log = sprintf(
            '%s, delete topic: %s, operationLog: %s',
            __METHOD__, $topic->id, $operateLog->id
        );
        logger($log);

        event(new TopicDeleted($topic, auth_user()));
    }

    /**
     * Handle the topic "restored" event.
     *
     * @param  \App\Models\Topic  $topic
     * @return void
     */
    public function restored(Topic $topic)
    {
        $operateLog = [
            'type' => TopicOperationLog::TYPE_RESTORE,
        ];
        $operateLog = $topic->operationLogs()->create($operateLog);
        $log = sprintf(
            '%s, restore topic: %s, operationLog: %s',
            __METHOD__, $topic->id, $operateLog->id
        );
        logger($log);

        event(new TopicRestored($topic, auth_user()));
    }

    /**
     * Handle the topic "force deleted" event.
     *
     * @param  \App\Models\Topic  $topic
     * @return void
     */
    public function forceDeleted(Topic $topic)
    {
        //
    }
}
